﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class LoginToSystemControl
    {
        UserRepository userRepo;
        RoleRepository roleRepo;
        EmployeeRepository employeeRepo;
        ActingManagerRepository actingManagerRepo;

        public LoginToSystemControl()
        {
            userRepo = new UserRepository();
            roleRepo = new RoleRepository();
            employeeRepo = new EmployeeRepository();
            actingManagerRepo = new ActingManagerRepository();
        }

        public List<string> ValidateUserByUsernameAndPassword(string username, string password)
        {
            List<string> list = new List<string>();

            User user = userRepo.GetUserByUsernameAndPassword(username, password);

            string redirectURL = "/Login.aspx";
            string result = "invaliduser";

            if (user != null)
            {
                if (user.RoleID > 0 && user.RoleID < 8)
                {
                    result = "validuser";
                    int userId = user.UserID;
                    int roleId = user.RoleID;
                    Employee employee = employeeRepo.GetEmployeeByUserId(userId);
                    int employeeId = employee.EmployeeID;

                    // TO be stored in session
                    string userIdString = userId.ToString();
                    string userName = user.UserName;
                    string roleIdString = roleId.ToString();
                    string roleName = user.Role.RoleName;
                    string employeeIdString = employeeId.ToString();
                    string employeeName = employee.EmployeeName;
                    string deptId = employee.DeptID;
                    string deptName = employee.Department.DeptName;

                    // Check if user(employee) is dept rep or acting manager
                    int deptRepId = 0;
                    if (employee.Department.RepresentativeID != null)
                    {
                        deptRepId = (int)employee.Department.RepresentativeID;
                    }
                    int actingManagerId = 0;
                    if (employee.Department.ActingManagerID != null)
                    {
                        actingManagerId = (int)employee.Department.ActingManagerID;
                    }

                    if (roleId == 1)
                    {
                        if (employeeId == deptRepId)
                        {
                            roleId = 2;
                            roleName = "Department Rep";
                        }
                        if (employeeId == actingManagerId)
                        {
                            // Check the start and end date as acting manager
                            DateTime todayDate = DateTime.Today.Date;
                            ActingManager actingManager = actingManagerRepo.GetLatestActingManagerByDepartmentID(deptId);
                            DateTime startDate = actingManager.StartDate.Value.Date;
                            DateTime endDate;
                            if (actingManager.EndDate.Value.Date != null)
                            {
                                endDate = actingManager.EndDate.Value.Date;
                            }
                            else
                            {
                                endDate = todayDate;
                            }
                            if (startDate <= todayDate && todayDate <= endDate)
                            {
                                roleId = 3;
                                roleName = "Acting Manager";
                            }
                        }
                    }

                    // Redirect to respective homepage
                    if (roleId == 5 || roleId == 6 || roleId == 7)
                    {
                        redirectURL = "/StoreHomepage.aspx";
                    }
                    else if (roleId == 1 || roleId == 2 || roleId == 3 || roleId == 4)
                    {
                        redirectURL = "/DeptHomepage.aspx";
                    }

                    // To be added to session
                    list.Add(result);
                    list.Add(redirectURL);
                    list.Add(userIdString);
                    list.Add(userName);
                    list.Add(employeeIdString);
                    list.Add(employeeName);
                    list.Add(roleIdString);
                    list.Add(roleName);
                    list.Add(deptId);
                    list.Add(deptName);
                }
                else
                {
                    list.Add(result);
                    list.Add(redirectURL);
                }
            }
            else
            {
                list.Add(result);
                list.Add(redirectURL);
            }
            return list;
        }
    }
}
